function data = generate_randn_data(d1, d2, true_rank, prop_known) 
% data = generate_randn_data(d1, d2, true_rank, prop_known, snr) 
%
% Generate a random matrix completion datalem of size d1-by-d2 and rank
% true_rank. A proportion prop_known of entries are selected uniformly
% and at random in the data matrix for learning.
%
% Ouput: 
%
%   ls and ts are a structures containing learning and testing data.
%
%   These structures contain three fields:
%     rows     list of row indices in the data matrix
%     cols     list of column indices in the data matrix
%     entries  list of corresponding entries
%
%     entries(i) is the element at position 
%     (rows(i),cols(i)) in the data matrix.


    
data.d1 = d1;
data.d2 = d2;
data.true_rank = true_rank;
     
  
m = round(d1*d2*prop_known);  
M.ind_Omega = randsampling(d1,d2,m)';
M.ind_Omega = M.ind_Omega(1:m); 
M.ind_Omega = sort(M.ind_Omega); %SVT,cvx

[data.ls.rows, data.ls.cols] = ind2sub([d1, d2],M.ind_Omega); %GROUSE

data.ts.rows = []; data.ts.cols = [];  

data.ls.nentries = length(data.ls.rows);
data.ts.nentries = length(data.ts.rows);
  
  
data.Gs = (randn(d1, true_rank));
data.Hs = (randn(d2, true_rank));
      
  
  data.ls.entries = partXY(data.Gs',data.Hs',data.ls.rows,data.ls.cols, data.ls.nentries)';
%   data.ts.entries = partXY(data.Gs',data.Hs',data.ts.rows,data.ts.cols,data.ts.nentries)';
  data.ts = [];
  


end


function omega = randsampling(n_1,n_2,m)

omega = ceil(rand(m, 1) * n_1 * n_2);
omega = unique(omega);
while length(omega) < m    
    omega = [omega; ceil(rand(m-length(omega), 1)*n_1*n_2);];
    omega = unique(omega);
end


end